package leetcode.weekly.week302;

import java.util.HashMap;
import java.util.Map;

//Solution2Test
public class Solution2 {
	// Solution2Test

	public int maximumSum(int[] nums) {
		Map<Integer, Integer> map = new HashMap<>();
		int ans = -1;
		for (int i = 0; i < nums.length; i++) {
			int num = nums[i];
			int bitSum = this.bitSum(num);
			if (map.containsKey(bitSum)) {
				int last = map.get(bitSum);
				int cur = last + num;
				ans = Math.max(ans, cur);
				map.put(bitSum, Math.max(last, num));
			} else {
				map.put(bitSum, num);
			}
		}
		return ans;
	}

	private int bitSum(int num) {
		int ans = 0;
		while (num > 0) {
			int last = num % 10;
			ans += last;
			num /= 10;
		}
		return ans;
	}

}
